package com.woodblockwithoutco.remotecontrollerexample;

import android.app.Activity;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.RemoteControlClient;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MusicWatcherActivity extends Activity implements OnClickListener {
    //Views in the Activity
    protected ImageButton mPrevButton;
    protected ImageButton mPlayPauseButton;
    protected ImageButton mNextButton;
    
    private boolean mMusicActive;
    
    private LocalBroadcastManager mLocalBroadcastManager;
    
    private IntentFilter mIntentFilter;
    
    
    private FakeMusicController mFakeMusicController;
    
    private BroadcastReceiver mMusicStateReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null || !MusicWatcherService.ACTION_MUSIC_STATE_CHANGED.equals(intent.getAction())) {
                return;
            }

            boolean musicActive = intent.getBooleanExtra(MusicWatcherService.EXTRA_STATE, false);
            updatePlayPauseState(musicActive);
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_watcher);
        mPrevButton = (ImageButton)findViewById(R.id.prev_button);
        mPlayPauseButton = (ImageButton)findViewById(R.id.play_pause_button);
        mNextButton = (ImageButton)findViewById(R.id.next_button);

        initBroadcastReceiver();
        
        mFakeMusicController = new FakeMusicController(this);
        
        final View buttons[] = { mPrevButton, mPlayPauseButton, mNextButton };
        for (View view : buttons) {
            view.setOnClickListener(this);
        }
    }
    
    private void initBroadcastReceiver() {
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
        mIntentFilter = new IntentFilter(MusicWatcherService.ACTION_MUSIC_STATE_CHANGED);
    }
    
    private void updatePlayPauseState(boolean active) {
        mMusicActive = active;
        mPlayPauseButton.setImageResource(active ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.prev_button:
                mFakeMusicController.sendPreviousEvent();
                break;
            case R.id.play_pause_button:
                if (mMusicActive) {
                    mFakeMusicController.sendPauseEvent();
                } else {
                    mFakeMusicController.sendPlayEvent();
                }
                break;
            case R.id.next_button:
                mFakeMusicController.sendNextEvent();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mLocalBroadcastManager.registerReceiver(mMusicStateReceiver, mIntentFilter);
        Intent intent = new Intent(this, MusicWatcherService.class);
        startService(intent);
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        mLocalBroadcastManager.unregisterReceiver(mMusicStateReceiver);
        Intent intent = new Intent(this, MusicWatcherService.class);
        stopService(intent);
    }
    

}
